<include file="header" />
<body>
<div class="wraper">
    <div class="header">
        <a href="javascript:history.go(-1);" class="iconfont icon-zuo back"></a>
        <div class="title text-center font-16">微信分账充值</div>
    </div>
    <div class="main-card">
        <div class="user flex">
            <div class="logo">
                <img src="/assets/base/StoredValue/images/logo.png" alt="">
            </div>
        </div>
        <div class="bg-logo">
            <img src="/assets/base/StoredValue/images/logo.png" alt="">
        </div>
    </div>
    <div class="classify">
        <p class="pay-hint gray">请选择充值金额</p>
        <div class="money flex">
            <div class="sum text-center sum-initial"><span class="amount">0.01</span>元</div>
            <div class="sum text-center sum-initial"><span class="amount">0.02</span>元</div>
            <div class="sum text-center sum-initial"><span class="amount">0.03</span>元</div>
            <div class="sum text-center sum-initial"><span class="amount">0.05</span>元</div>
            <div class="sum text-center sum-initial"><span class="amount">1.00</span>元</div>
        </div>
    </div>
    <a class="btn text-center font-16" id="recharge">充值</a>
</div>
</body>
<include file="loadding" />

<include file="commonJs" />

<script>
    $(function () {
        var height = $(window).height();
        $('.wraper').css('minHeight', height);

        $('.money').on('click', '.sum', function () {
            $(this).removeClass('sum-initial').addClass('sum-actived').siblings().removeClass('sum-actived').addClass('sum-initial');
        });
        //控制按钮只能点一次
        var one_click = 0;
        $('.btn').click(function () {
            if ($(".sum-actived").length <= 0) {
                fm.alert("请选择充值金额");
                return false;
            }
            one_click++;
            if (one_click > 1) {
                return false;
            }
            var amount = $(".sum-actived").find(".amount").text();
            $.ajax({
                url: '/base/WechatLedger/orderPay/account_id/'+"{BEESCRM:$account_id}",
                type: 'POST',
                dataType: 'json',
                data: {amount: amount},
                success: function (data) {
                    if (data.status == 'AS') {
                        location.href = data.url;
                    } else if (data.status == 'WS') {
                        //微信支付
                        if (typeof WeixinJSBridge == "undefined") {
                            if (document.addEventListener) {
                                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
                            } else if (document.attachEvent) {
                                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
                            }
                        } else {
                            WeixinJSBridge.invoke(
                                    'getBrandWCPayRequest',
                                    $.parseJSON(data.url),
                                    function (res) {
                                        if (res.err_msg == "get_brand_wcpay_request:ok") {
                                            paySuccess(data.order_id);
                                        } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                            one_click = 0;
                                            fm.alert('你肯定不是真的想抛弃我们～');
                                            return false;
                                        } else {
                                            one_click = 0;
                                            fm.alert('哎呀，网络不给力，小微又调皮了，咋们刷新再来一次');
                                            setTimeout("location.reload()", 2000);
                                            return false;
                                        }
                                    }
                            );
                        }
                    } else if (data.status == 'S') {
                        paySuccess(data.order_id);
                    } else {
                        one_click = 0;
                        fm.alert(data.message);
                    }
                }
            })
        });

        //支付成功
        function paySuccess(order_id) {
            var account_id ="{BEESCRM:$account_id}";
            $.ajax({
                url: "/base/WechatLedger/paySuccess/order_id/"+order_id,
                type: "POST",
                dataType: 'json',
                data: {account_id:account_id},
                success: function (res) {
                    if (res.status == 'S') {
                        fm.alert(res.message, "", 2, "");
                        window.sessionStorage.setItem("local_url",res.url);
                        one_click = 0;
                        setTimeout(redirect, 2000);
                    } else {
                        one_click = 0;
                        fm.alert(res.message);
                    }
                }
            });
        }
    });
    function redirect() {
        window.location.href = window.sessionStorage.getItem("local_url");
        window.sessionStorage.setItem("local_url",null);
    }
</script>
</html>